(function(_scope){
	/*
	 * pi.win.js
	 * Window plug-in for pi.js
	 * 1.0
	 * Azer Koçulu <http://azer.kodfabrik.com>
	 * http://pi.kodfabrik.com
	 */
	_scope.win = pi.base.extend({
		"$Init":function(_element){
			
			if(_element){
				_element = _element.pi||(_element.element?_element:new pi.element(_element));
			};
			
			this.element.setContainer( _element||new pi.element("div") ).element.getContainer().attribute.addClass("Window");
			this.element.setBody( new pi.element("div").attribute.addClass("Body").insert(this.element.getContainer()) );
			this.element.setTitlebar( new pi.element("div").attribute.addClass("Titlebar").insert( new pi.element("div").attribute.addClass("TitlebarBorder").insert(this.element.getBody()) ) );
			this.element.setContent( new pi.element("div").attribute.addClass("Content").insert(new pi.element("div").attribute.addClass("ContentBorder").insert(this.element.getBody())) );
			this.element.setStatusbar( new pi.element("div").attribute.addClass("Statusbar").insert(new pi.element("div").attribute.addClass("StatusbarBorder").insert(this.element.getBody())) );
			
			this.environment.setLayer( new pi.layer(this.element.getContainer(), this.element.getTitleText(), this.element.getResizer() ) );
			
		},
		"content":null,
		"fixContentHeight":function(){
			var layer = this.environment.getLayer(), content=this.element.getContent().element.parentNode.pi, width = layer.environment.getWidth(), height = layer.environment.getHeight(),
			titlebarHeight = this.element.getTitlebar().element.parentNode.offsetHeight, statusbarHeight = this.element.getStatusbar().element.parentNode.offsetHeight,
			contentView = content.environment.getView(), contentPadding = parseInt(contentView.paddingTop)+parseInt(contentView.paddingBottom), contentBorder=parseInt(contentView.borderTopWidth)+parseInt(contentView.borderBottomWidth),
			availableSpace = height-(titlebarHeight+statusbarHeight+contentPadding+contentBorder);
			content.environment.addStyle({ "height":availableSpace+"px" });
			return this;
		},
		"setHeight":function(_value){
			this.environment.getLayer().environment.setHeight(_value);
			return this;
		},
		"setWidth":function(_value){
			this.environment.getLayer().environment.setWidth(_value);
			return this;
		},
		"show":function(_x,_y){
			if(_x)this.environment.getLayer().environment.setLeft(_x);
			if(_y)this.environment.getLayer().environment.setTop(_y);
			this.element.getContainer().attribute.addClass("Window_Visible");
			this.fixContentHeight();
		},
		"hide":function(){
			this.element.getContainer().attribute.removeClass("Window_Visible");
		},
		"remove":function(){
			this.element.getContainer().remove();
		},
		"element":{
			"_closer":null, "_container":null, "_content":null, "_body":null, "_resizer":null, "_statusbar":null, "_titlebar":null, "_titleText":null,
			"setContent":function(_value){
				this._parent_.content=_value.element;
				this._setContent(_value);
			},
			"setStatusbar":function(_value){
				this.setResizer(new pi.element("div").attribute.addClass("Resizer").insert(_value));
				this._setStatusbar(_value);
			},
			"setTitlebar":function(_value){
				this.setCloser(new pi.element("A").event.addListener("click",this._parent_.remove.curry(this._parent_)).attribute.addClass("Closer").insert(_value));
				this.setTitleText(new pi.element("div").attribute.addClass("TitleText").update(this._parent_.environment.getTitle()).insert(_value));
				this._setTitlebar(_value);
			}
		},
		"environment":{
			"_layer":null, "_title":"Untitled",
			"setLayer":function(_value){
				var view = _value.environment.getElement().environment.getView();
				_value.event.resize = this._parent_.event.resize.curry(this._parent_);
				_value.environment._setHeight(parseInt(view.height));
				_value.environment._setWidth(parseInt(view.width));
				_value.environment._setLeft(parseInt(view.left));
				_value.environment._setTop(parseInt(view.top));
				this._setLayer(_value);
			},
			"setTitle":function(_value){
				this._parent_.element.getTitleText().update(_value);
				this._setTitle(_value);
				return this._parent_;
			}
		},
		"event":{
			"resize":function(_layer){
				this.fixContentHeight();
			}
		}
	});
})(pi);